<?php


namespace app\console\controller;


use app\BaseController;
use app\console\service\machiningScheme\MachiningSchemeQueryService;
use app\console\service\machiningScheme\MachiningSchemeService;
use app\console\validate\MachiningSchemeValidate;
use app\lib\exception\ParamException;
use app\model\MachiningScheme;
use app\Request;
use think\App;
use think\exception\ValidateException;

function machiningScheme2dict($list)
{
    $dateTime = app('DateTime');

    return [
        'id' => $list['id'],
        'name' => $list['name'],
        'ratio' => $list['ratio'],
        'create_time' => $list['create_time'],
        'update_time' => $list['update_time'],
        'format_create_time' =>  $dateTime->timestamp2date($list['create_time']),
        'format_update_time' =>  $dateTime->timestamp2date($list['update_time']),
    ];
};

//供应商
class MachiningSchemeController extends BaseController
{
    public function create(Request $request, MachiningSchemeService $machiningSchemeService)
    {
        $param = $request->param(['name', 'ratio']);
        try
        {
            validate(MachiningSchemeValidate::class)
                ->scene('create')
                ->check($param);
        } catch (ValidateException $e) {
            throw new ParamException($e->getMessage());
        }

        $mschiningScheme = $machiningSchemeService->create($param);

        return $this->returnJson(machiningScheme2dict($mschiningScheme));
    }

    public function get(Request $request, MachiningSchemeQueryService $machiningSchemeQueryService)
    {
        $all = $request->get('all', false);
        $page = $request->get('page', 1);
        $perPage = $request->get('perPage', config('app.allow_limit'));

        if (!$all)
        {
            try
            {
                validate(MachiningSchemeValidate::class)
                    ->scene('get')
                    ->check([
                        'page'         => $page ,
                        'perPage'      => $perPage ,
                    ]);
            } catch (ValidateException $e) {
                throw new ParamException($e->getMessage());
            }

            $machiningschemes = $machiningSchemeQueryService->getPageList($page ,$perPage);
        }else{
            $machiningschemes = $machiningSchemeQueryService->getList();
        }

        return $this->returnList(
            array_map(fn($list) => machiningScheme2dict($list), $machiningschemes['list']->toArray()),
            $machiningschemes['pagination']['page'], $machiningschemes['pagination']['perPage'], $machiningschemes['pagination']['total']
        );
    }

    public function update(Request $request, MachiningSchemeService $machiningSchemeService)
    {
        $param = $request->param(['name', 'ratio'], '');

        if (empty($param))
        {
            throw new ParamException('未修改任何数据');
        }

        $mschiningScheme = $machiningSchemeService->update($param);

        return $this->returnJson(machiningScheme2dict($mschiningScheme));
    }

    public function delete(MachiningSchemeService $machiningSchemeService)
    {
        $machiningSchemeService->delete();
        return $this->returnJson();
    }

    public function getById(MachiningScheme $machiningScheme)
    {
        return $this->returnJson(machiningScheme2dict($machiningScheme));
    }
}